import 'dart:async';

import 'package:bloc_usage/bloc/Bloc.dart';
import 'package:bloc_usage/model/entities.dart';
import 'package:bloc_usage/model/response_wrapper.dart';
import 'package:bloc_usage/util/HttpUtil.dart';
import 'package:dio/dio.dart';

class MainBloc extends Bloc {
  Dio _dio = Dio();

  // Main Page--> Article
  List<Article> _articlesList = List();
  MainModel mainModel = MainModel();
  StreamController<MainModel> _mainStreamController =
      StreamController<MainModel>.broadcast();

  get mainStream => _mainStreamController.stream.asBroadcastStream();

  get mainSink => _mainStreamController.sink;

  @override
  void release() {
    // _mainStreamController.close();
  }

  void init() async{
    // await HttpUtil.get(path)
  }

  void getArticleList(int page) async {
   ResponseWrapper response = await HttpUtil.get("https://www.wanandroid.com/article/list/$page/json");
    if (response.data != null) {
        
        Map<String, dynamic> data = response.data;
        List datas = data["datas"];
        List<Article> articles = datas.map((d) => Article.fromJson(d)).toList();
        mainModel.articles = articles;
        print("-------------------------->articles: ${articles.length}");
        articles.forEach((article){
            print("article title :${article.title}");
        });
        mainSink.add(mainModel);
      }
  }
}
